<?php
/**
 * Created by PhpStorm.
 * User: ming
 * Date: 17-8-16
 * Time: 上午10:41
 */
$host = '127.0.0.1';
$port = 9501;
$serv = new swoole_server($host, $port,SWOOLE_PROCESS,SWOOLE_SOCK_TCP);


//监听连接进入事件
$serv->on('connect', function ($serv, $fd) {
//    echo "建立连接\n";
    $serv->tick(1000, function() use ($serv, $fd) {
        $serv->send($fd, "这是一条定时消息\n");
    });
});

//监听数据发送事件
$serv->on('receive', function ($serv, $fd, $reactor_id, $data) {
//    $serv->send($fd, 'Tcp返回成功:' . $data."\n");
//    echo "Receive message:$data";
    switch ($data) {
        case 1: {
            foreach($serv->connections as $tempFD)
            {
                # 注: $tempFD 是全体client, $fd 是当前client.
                $serv->send($tempFD,"client {$fd} say : 1 for apple\n");
            }
            break;
        }
        case 2: {
            $serv->send($fd, "2 for boy\n");
            break;
        }
        default: {
            $serv->send($fd,"Others is deault\n");
            break;
        }
    }

});

//监听连接关闭事件
$serv->on('close', function ($serv, $fd) {
    echo "\n连接关闭";
});

$serv->start();//启动服务器